/**
 *  Stream Recorder: Records RealPlayer(tm) streams as MP3s
 *  Copyright (C) 2007  David Griffiths
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package dg.radio2ipod.model;

import dg.radio2ipod.Main;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.AbstractListModel;


/**
 * Model for the stream list.
 * @author davidg
 */
public final  class StreamList extends AbstractListModel
        implements ActionListener, StreamListener {
    /**
     * Internationalization strings.
     */
    private static ResourceBundle resBundle = ResourceBundle.getBundle(
            "dg/radio2ipod/resource/strings");

    private ArrayList streams = new ArrayList();
    static final String DEST_FOLDER = System.getProperty("user.home")
    + resBundle.getString("/Desktop/");

    /**
     * Constructor for an empty stream list.
     */
    public StreamList() {
    }

    /**
     * Get the stream at position i.
     * @param i position in the list, starts at 0
     * @return relevant stream object
     */
    public Object getElementAt(final int i) {
        return streams.get(i);
    }

    /**
     * Remove the stream at position i from the list
     * @param i position in list, starting at 0
     */
    public void remove(final int i) {
        Stream stream = (Stream) streams.get(i);
        System.out.println("stream is " + stream);
        if ((stream.getStatus() == Stream.BUFFERING)
        || (stream.getStatus() == Stream.READING)) {
            System.out.println("going to kill " + stream);
            stream.stopAllRecording();
        }
        streams.remove(i);
        if (i == 0) {
            saveNext();
        }
        fireContentsChanged(this, i, streams.size() + 1);
        this.fireIntervalRemoved(this, i, streams.size() + 1);
    }

    /**
     * Current length of list
     * @return length of list
     */
    public int getSize() {
        return streams.size();
    }

    /**
     * Start the top stream of the list (position 0) saving.
     */
    public void saveNext() {
        if (streams.size() == 0) {
            return;
        }
        Stream s = (Stream) streams.get(0);
        if (s.getStatus() == Stream.UNSTARTED) {
            File dest = new File(DEST_FOLDER + s.getSourceBaseName() + ".mp3");
            s.addListener(this);
            s.save(dest, this);
        }
    }

    public void receiveLine(String line) {
        Main.getMainFrame().consoleWrite(line);
    }

    /**
     * Add a stream to the end of the list.
     * @param stream stream to add
     */
    public void add(final Stream stream) {
        streams.add(stream);
        if (streams.size() == 1) {
            saveNext();
        }
        fireContentsChanged(this, 0, 1);
        fireIntervalAdded(this, 0, 1);
    }

    /**
     * Called when the status of a stream has changed. Deletes
     * the stream from the list if it has finished or failed.
     * @param e action-event describing what has happened to the
     * stream
     */
    public void actionPerformed(final ActionEvent e) {
        Stream s = (Stream) e.getSource();
        fireContentsChanged(this, 0, 1);
        if ((s.getStatus() == Stream.FAILED)
        || (s.getStatus() == Stream.SUCCESSFUL)) {
            streams.remove(s);
            fireIntervalRemoved(this, 0, 1);
            saveNext();
        }
    }
}
